/*
Copyright (c) 2008 SmashedApples 

Contributors.  See: http://smashedapples.googlecode.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package smashedapples.managers
{
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class KeyboardManager extends EventDispatcher 
	{	
		public static const COMBO_CTRL : String = "ctrlKey";
		public static const COMBO_SHIFT : String = "shiftKey";
		public static const COMBO_ALT : String = "altKey";
		
		public static const BACKSPACE : uint = 8;
		public static const CAPS_LOCK : uint = 20;
		public static const	CONTROL : uint = 17;
		public static const DELETE : uint = 46;
		public static const DOWN : uint = 40;
		public static const END : uint = 35;
		public static const ENTER : uint = 13;
		public static const ESCAPE : uint = 27;
		public static const F1 : uint = 112;
		public static const F10 : uint = 121;
		public static const	F11 : uint = 122;
		public static const F12 : uint = 123;
		public static const	F13 : uint = 124;
		public static const F14 : uint = 125;
		public static const F15 : uint = 126;
		public static const F2 : uint = 113;
		public static const	F3 : uint = 114;
		public static const F4 : uint = 115;
		public static const	F5 : uint = 116;
		public static const	F6 : uint = 117;
		public static const F7 : uint = 118;
		public static const F8 : uint = 119;
		public static const	F9 : uint = 120;
		public static const HOME : uint = 36;
		public static const INSERT : uint = 45;
		public static const	LEFT : uint = 37;
		public static const	NUMPAD_0 : uint = 96;
		public static const	NUMPAD_1 : uint = 97;
		public static const	NUMPAD_2 : uint = 98;
		public static const	NUMPAD_3 : uint = 99;
		public static const	NUMPAD_4 : uint = 100;
		public static const	NUMPAD_5 : uint = 101;
		public static const	NUMPAD_6 : uint = 102;
		public static const	NUMPAD_7 : uint = 103;
		public static const	NUMPAD_8 : uint = 104;
		public static const	NUMPAD_9 : uint = 105;
		public static const	NUMPAD_ADD : uint = 107;
		public static const NUMPAD_DECIMAL : uint = 110;	
		public static const	NUMPAD_DIVIDE : uint = 111;
		public static const	NUMPAD_ENTER : uint = 108;
		public static const	NUMPAD_MULTIPLY : uint = 106;
		public static const	NUMPAD_SUBTRACT : uint = 109;
		public static const	PAGE_DOWN : uint = 34;
		public static const	PAGE_UP : uint = 33;
		public static const RIGHT : uint = 39;
		public static const SHIFT : uint = 16;
		public static const	SPACE : uint = 32;
		public static const TAB : uint = 9;
		public static const UP : uint = 38;
		
		public static const A : uint = 65;
		public static const S : uint = 83;
		public static const Y : uint = 89;
		public static const M : uint = 77;
		
		
		public static function addKeyListener(target:DisplayObject,type:String,listener:Function,key:uint):void
		{
			var keyListener : KeyListener = new KeyListener(listener,key);
			target.addEventListener(type,keyListener.keyHandler);
		}
		
		public static function addKeyComboListener(target:DisplayObject,type:String,listener:Function,key1:String,key2:uint):void
		{
			var keyComboListener : KeyComboListener = new KeyComboListener(listener,key1,key2);
			target.addEventListener(type,keyComboListener.keyHandler);
		}	
	
	}
}
	
	
import flash.events.KeyboardEvent;
	
class KeyComboListener
{
	public var key1 : String;
	public var key2 : uint;
	public var listener : Function;
	public function KeyComboListener(listener:Function,key1:String,key2:uint):void
	{
		this.listener = listener;
		this.key1 = key1;
		this.key2 = key2;
		
	}
	public function keyHandler(event:KeyboardEvent):void
	{	
		if(event[this.key1])
		{	
			if (event.keyCode == this.key2)
			{
				listener(event);
			}
		} 
			
	}
} 
	

import flash.events.KeyboardEvent;
	
class KeyListener
{
	public var key : uint;
	public var listener : Function;
	public function KeyListener(listener:Function,key:uint):void
	{
		this.listener = listener;
		this.key = key;
		
	}
	public function keyHandler(event:KeyboardEvent):void
	{	
		
		if(event.keyCode == this.key)
		{
			listener(event);
		}
			
	}
} 
